home *** CD-ROM | disk | FTP | other *** search
/ EnigmA Amiga Run 1999 March / EnigmA AMIGA RUN 35 (1999)(G.R. Edizioni)(IT)[!][issue 1999-03].iso / earcd / devel / vbcc-68k-src / machines / amiga68k / libsrc / ixemul / crt0.asm next >
Assembly Source File  |  1999-01-01  |  10KB  |  436 lines

  1. **
  2. **  crt0.asm / ixemul startup code for vbcc/PhxLnk
  3. **
  4. **  This startup code will need at least PhxLnk v4.30 to find all
  5. **  the required linker symbols.
  6. **
  7. **  Based on crt0.c by Loren J. Rittle
  8. **  Written by Frank Wille 1997
  9. **
  10. **  Assemble with BASECRT0 set, to generate the small data version.
  11. **
  12. **  started: 07-Jan-97
  13. **
  14. ** v0.1 (08-Jan-97) hard-coded __stk_limit, __stk_argbytes and __machtype,
  15. **                  no small data support, no moncontrol support for
  16. **                  profiling, etc... ;)
  17. ** v0.2 (09-Jan-97) omitting the __init_stk_limit()-call seems to be a better
  18. **                  solution for the __stk_limit/__stk_argbytes problem (?)
  19. ** v0.9 (10-Jan-97) removed some obsolete xdefs, minor code optimizations,
  20. **                  small data support by setting BASECRT0 (!)
  21. ** v1.0 (02-Feb-97) supports some (all?) nice ixemul.library v45 features
  22. **                  (e.g. automatic stack extension, some network ini-
  23. **                  tialization, etc.) so that v45 is *required* from now
  24. **                  on,
  25. **                  ___ixemulVer may be defined by the user to set the
  26. **                  mininum ixemul-version, which the program requires,
  27. **                  besides ___ixemulVer, some other symbols have moved
  28. **                  into the ixemul.lib to give the user the possibility
  29. **                  to overwrite them by an own definition: _expand_cmd_line,
  30. **                  ___stk_limit, ___stk_argbytes and ___stack,
  31. **                  the __init_stk_limit()-call is in the startup code again,
  32. **                  BASECRT0 mode needs the exec-entries a_bss set to zero
  33. **                  and a_data = _DATA_LEN_ + _BSS_LEN_, this is done by
  34. **                  importing the new linker symbol _SMALL_DATA_LEN_, which
  35. **                  PhxLnk defines since v4.30
  36. **
  37.  
  38. IX_VERSION    equ    45        ; minimum version for startup code
  39.  
  40. ; exec.library LVOs
  41. OpenLibrary    equ    -552
  42. CloseLibrary    equ    -414
  43. Forbid        equ    -132
  44. GetMsg        equ    -372
  45. ReplyMsg    equ    -378
  46. WaitPort    equ    -384
  47. CopyMem        equ    -624
  48.  
  49. ; dos.library LVOs
  50. FilePart    equ    -870
  51.  
  52. ; intuition.library LVOs
  53. EasyRequestArgs    equ    -588
  54.  
  55. ; ixemul.library LVOs
  56. ix_startup    equ    -552
  57. ix_exec_entry    equ    -1896
  58. ix_get_vars2    equ    -1902
  59. ix_resident    equ    -2568
  60. __init_stk_limit equ    -2904
  61. __stkext_startup equ    -3450
  62.  
  63.  
  64.     include    "exec/execbase.i"
  65.     include    "exec/libraries.i"
  66.     include    "dos/dosextens.i"
  67.     include    "intuition/intuition.i"
  68.  
  69.  
  70.  
  71.     code
  72.  
  73.     xref    _main
  74.  
  75.     xref    _CODE_LEN_        ; linker symbols, generated by PhxLnk
  76.     xref    _DATA_LEN_
  77.     xref    _BSS_LEN_
  78. ;    xref    ___machtype
  79. ___machtype    equ    0        ; currently hard-coded
  80.  
  81.  
  82.     IFD    BASECRT0
  83.     near    a4,-2            ; activate small data mode
  84.     xref    _SMALL_DATA_LEN_    ; total small data size
  85.     nref    _expand_cmd_line
  86.     nref    ___stk_limit
  87.     nref    ___stk_argbytes
  88.     nref    ___ixemulVer
  89.     nref    ___stack
  90.     ELSE
  91.     xref    _expand_cmd_line    ; expand wildcards?
  92.     xref    ___stk_limit
  93.     xref    ___stk_argbytes
  94.     xref    ___ixemulVer        ; ixemul.library version to open
  95.     xref    ___stack        ; desired stack size
  96.     ENDIF
  97.  
  98. ; This is the first code executed.  Note that a_magic has to be at
  99. ; a known offset from the start of the code section in order for
  100. ; execve() to know that the program that it is starting uses the
  101. ; ixemul library.
  102.  
  103.     bra.w    _ENTRY
  104.  
  105. ; this is a struct exec, for now only OMAGIC is supported 
  106.     dc.w    ___machtype     ; a_mid 
  107.     dc.w    @407        ; a_magic = OMAGIC
  108.     dc.l    _CODE_LEN_    ; a_text 
  109.     IFND    BASECRT0
  110.     dc.l    _DATA_LEN_    ; a_data 
  111.     dc.l    _BSS_LEN_    ; a_bss 
  112.     ELSE
  113.     dc.l    _SMALL_DATA_LEN_
  114.     dc.l    0
  115.     ENDIF
  116.     dc.l    0        ; a_syms 
  117.     dc.l    _exec_entry    ; a_entry 
  118.     dc.l    0        ; a_trsize 
  119.     dc.l    0        ; a_drsize 
  120.  
  121.  
  122. abort_txt:
  123.     dc.b    "Abort",0
  124. liberr_txt:
  125.     dc.b    "Need at least version %ld of ixemul.library.",0
  126. crt_txt:
  127.     IFD    BASECRT0
  128.     dc.b    "b"
  129.     ENDIF
  130.     dc.b    "crt0",0
  131.     even
  132.  
  133.  
  134.     xdef    _ix_get_variables    ; should this be extern?
  135. _ix_get_variables:
  136. ; int from_vfork_setup_child (0 or 1)
  137.     move.l    _ixemulbase,a0
  138.     pea    __res_socket
  139.     lea    __res,a1        ; set resolver state default settings
  140.     move.l    a1,-(sp)
  141.     move.l    #5,(a1)+        ; retrans = RES_TIMEOUT
  142.     move.l    #4,(a1)+        ; retry = 4
  143.     move.l    #$2c0,(a1)+        ; options = RES_DEFAULT
  144.     move.l    #1,(a1)+        ; nscount = 1
  145.     pea    _h_errno
  146.     tst.l    4*4(sp)            ; from_vfork_setup_child ?
  147.     beq    .1
  148.     pea    _errno
  149.     pea    _environ
  150.     bra    .2
  151. .1:    clr.l    -(sp)
  152.     clr.l    -(sp)
  153. .2:    pea    _environ
  154.     pea    ___sF
  155.     pea    _DOSBase
  156.     pea    _SysBase
  157.     pea    _sys_nerr
  158.     pea    __ctype_
  159.     pea    11.w
  160.     jsr    ix_get_vars2(a0)
  161.     add.w    #48,sp
  162.     rts
  163.  
  164.  
  165. _start_stdio:
  166. ; int argc, char **argv, char **env
  167.     move.l    a6,-(sp)
  168.     clr.l    -(sp)
  169.     bsr    _ix_get_variables    ; init SysBase, DOSBase, etc.
  170.  
  171. ; we call a v36 dos.library function here, because crt0 should have
  172. ; already failed on a pre-v36 system.
  173.     move.l    _DOSBase,a6
  174.     move.l    4*4(sp),a0
  175.     move.l    (a0),d1            ; argv[0]
  176.     beq    .1
  177.     jsr    FilePart(a6)        ; get program name
  178.     move.l    d0,___progname
  179.  
  180. ; call main
  181. .1:    movem.l    3*4(sp),d0-d1/a0    ; argc, argv, env
  182.     move.l    a0,_environ
  183.     movem.l    d0-d1/a0,-(sp)
  184.     jsr    _main            ; call main(argc,argv,env) entry point
  185.     add.w    #16,sp
  186.     move.l    (sp)+,a6
  187.     rts
  188.  
  189.  
  190. _exec_entry:
  191. ; struct Library *ibase, int argc, char *argv[], char *env[]
  192.     IFD    BASECRT0
  193.     bsr    _geta4
  194.     move.l    4(sp),a0        ; ibase
  195.     move.l    a4,-(sp)
  196.     pea    2.w
  197.     jsr    ix_resident(a0)
  198.     addq.w    #8,sp
  199.     ENDIF
  200.     move.l    4(sp),a0        ; ibase
  201.     move.l    a0,_ixemulbase        ; set ixemulbase
  202.     move.l    ___ixemulVer,d0
  203.     cmp.w    LIB_VERSION(a0),d0
  204.     bls    .1            ; version ok?
  205.  
  206.     move.l    4.w,a6
  207.     move.l    d0,-(sp)
  208.     pea    abort_txt(pc)
  209.     pea    liberr_txt(pc)
  210.     pea    crt_txt(pc)
  211.     subq.w    #8,sp
  212.     bsr    ix_panic
  213.     add.w    #24,sp
  214.     move.l    #(20<<8)|0,d0        ; W_EXITCODE(20,0)
  215.     rts
  216.  
  217. .1    move.l    a0,-(sp)
  218.     move.l    ___stk_argbytes,-(sp)
  219.     pea    ___stk_limit
  220.     jsr    __init_stk_limit(a0)
  221.     addq.w    #8,sp
  222.     move.l    (sp)+,a0
  223.     pea    _start_stdio(pc)
  224.     pea    _errno
  225.     move.l    24(sp),-(sp)        ; env
  226.     move.l    24(sp),-(sp)        ; argv
  227.     move.l    24(sp),-(sp)        ; argc
  228.     move.l    ___stack,d0        ; desired stack size set?
  229.     beq    .2
  230.     jsr    __stkext_startup(a0)    ; stack extension, if required
  231. .2    jsr    ix_exec_entry(a0)
  232.     add.w    #20,sp
  233.     rts
  234.  
  235.  
  236.     IFD    BASECRT0
  237.     xref    _DATA_BAS_
  238.     xdef    _geta4
  239. _geta4:
  240.     lea    _DATA_BAS_+32766,a4    ; init small data base register
  241.     rts
  242.     ENDIF
  243.  
  244.  
  245. ; Note: This routine must be far enough away from the start of code
  246. ; so that the PC relative offset won't fit in a byte and the assembler
  247. ; will generate the right instruction pattern that execve() is looking
  248. ; for to know that this is a program that uses the ixemul.library.
  249.  
  250. _ENTRY:
  251.     IFD    BASECRT0
  252.     movem.l    d2/a2/a4/a6,-(sp)
  253.     bsr    _geta4
  254.     ELSE
  255.     movem.l    d2/a2/a6,-(sp)
  256.     ENDIF
  257.     move.l    a0,d2            ; a2/d2 save command line
  258.     move.l    d0,a2
  259.     move.l    4.w,a6            ; ExecBase (v37)
  260.     cmp.w    #37,LIB_VERSION(a6)    ; required for StackSwap()
  261.     blo    .6
  262.     move.l    ___ixemulVer,d0        ; user-defined version
  263.     moveq    #IX_VERSION,d1        ; minimum version required
  264.     cmp.l    d1,d0
  265.     bhs    .4
  266.     move.l    d1,d0
  267.     move.l    d0,___ixemulVer
  268. .4:    lea    ixemul_name(pc),a1
  269.     jsr    OpenLibrary(a6)        ; open ixemul.library
  270.     move.l    d0,_ixemulbase
  271.     beq    .1
  272.  
  273.     move.l    d0,a6            ; a6 ixemulbase
  274.     IFD    BASECRT0
  275.     move.l    a4,-(sp)
  276.     pea    2.w
  277.     jsr    ix_resident(a6)
  278.     addq.w    #8,sp
  279.     ENDIF
  280.     move.l    ___stk_argbytes,-(sp)
  281.     pea    ___stk_limit
  282.     jsr    __init_stk_limit(a6)
  283.     pea    _errno
  284.     pea    _start_stdio(pc)
  285.     move.l    _default_wb_window,-(sp)
  286.     move.l    _expand_cmd_line,-(sp)
  287.     move.l    a2,-(sp)
  288.     move.l    d2,-(sp)
  289.     move.l    ___stack,d0        ; desired stack size set?
  290.     beq    .5
  291.     jsr    __stkext_startup(a6)    ; stack extension, if required
  292. .5:    jsr    ix_startup(a6)        ; ixemul startup
  293.  
  294.     move.l    d0,d2
  295.     add.w    #32,sp
  296.     move.l    a6,a1
  297.     move.l    4.w,a6
  298.     jsr    CloseLibrary(a6)    ; close ixemul.library
  299.     move.l    d2,d0
  300.     bra    .3
  301.  
  302. .1:    move.l    ___ixemulVer,-(sp)    ; error on opening ixemul.library
  303.     pea    abort_txt(pc)
  304.     pea    liberr_txt(pc)
  305.     pea    crt_txt(pc)
  306.     subq.w    #8,sp
  307.     bsr    ix_panic
  308.     add.w    #24,sp
  309. .6:    move.l    ThisTask(a6),a2
  310.     tst.l    pr_CLI(a2)        ; started from CLI?
  311.     bne    .2
  312. ; Quickly deal with the WB startup message, as the library couldn't do
  313. ; this for us. Nothing at all is done that isn't necessary to just shutup
  314. ; workbench.
  315.     jsr    Forbid(a6)
  316.     add.w    #pr_MsgPort,a2
  317.     move.l    a2,a0
  318.     jsr    WaitPort(a6)
  319.     move.l    a2,a0
  320.     jsr    GetMsg(a6)
  321.     move.l    d0,a1
  322.     jsr    ReplyMsg(a6)        ; reply WBStartup message
  323. .2:    moveq    #20,d0
  324. .3:    
  325.     IFD    BASECRT0
  326.     movem.l    (sp)+,d2/a2/a4/a6
  327.     ELSE
  328.     movem.l    (sp)+,d2/a2/a6
  329.     ENDIF
  330.     rts
  331.  
  332. ixemul_name:
  333.     dc.b    "ixemul.library",0
  334. intuition_name:
  335.     dc.b    "intuition.library",0
  336.     even
  337.  
  338.  
  339. ix_panic:
  340. ; Displays a v36 easy-requester.
  341. ; A comlete EasyStruct is already on stack, when called. Only es_StructSize
  342. ; and es_Flags will be initialized here.
  343. ; a6 = ExecBase
  344.     lea    intuition_name(pc),a1
  345.     moveq    #36,d0
  346.     jsr    OpenLibrary(a6)        ; open intuition.library v36
  347.     tst.l    d0
  348.     beq    .1
  349.     movem.l    a2-a3/a6,-(sp)
  350.     move.l    d0,a6            ; IntuitionBase
  351.     sub.l    a0,a0
  352.     lea    4*4(sp),a1        ; EasyStruct
  353.     move.l    #EasyStruct_SIZEOF,es_StructSize(a1)
  354.     clr.l    es_Flags(a1)
  355.     sub.l    a2,a2
  356.     lea    EasyStruct_SIZEOF(a1),a3 ; ArgList
  357.     jsr    EasyRequestArgs(a6)    ; render panic requester
  358.     move.l    a6,a1
  359.     movem.l    (sp)+,a2-a3/a6
  360.     jsr    CloseLibrary(a6)    ; close intuition.library
  361. .1:    rts
  362.  
  363.  
  364. ; null mcount and moncontrol, just in case some routine is 
  365. ; compiled for profiling
  366.     xdef    mcount
  367.     xdef    _moncontrol
  368. _moncontrol:
  369. mcount: 
  370.     rts 
  371.  
  372.  
  373.  
  374.     IFD    BASECRT0
  375.     section    __MERGED,data
  376.     ELSE
  377.     data
  378.     ENDIF
  379.  
  380. _environ:
  381.     dc.l    dummy_environ ; default for progs not started via exec_entry
  382. ___progname:
  383.     dc.l    dummy_environ ; use dummy_environ as empty string
  384. __res_socket:        ; resolv socket used for communications
  385.     dc.l    -1
  386.  
  387.     xdef    _environ
  388.     xdef    ___progname
  389.     xdef    __res_socket
  390.  
  391.  
  392.  
  393.     IFD    BASECRT0
  394.     section    __MERGED,bss
  395.     ELSE
  396.     bss
  397.     ENDIF
  398.  
  399. _ixemulbase:
  400.     ds.l    1
  401. _errno:            ; error results from the library
  402.     ds.l    1
  403. __ctype_:
  404.     ds.l    1
  405. _sys_nerr:        ; number of system error codes
  406.     ds.l    1
  407. _SysBase:
  408.     ds.l    1
  409. _DOSBase:
  410.     ds.l    1
  411. ___sF:
  412.     ds.l    1
  413. ___SaveSP:
  414.     ds.l    1
  415. dummy_environ:
  416.     ds.l    1
  417. _default_wb_window:    ; Default Workbench output window name
  418.     ds.l    1
  419. _h_errno:
  420.     ds.l    1    ; networking error code
  421. __res:            ; resolver state
  422.     ds.b    448    ; sizeof(struct __rest_state) + some extra bytes
  423.  
  424.     xdef    _ixemulbase
  425.     xdef    _errno
  426.     xdef    __ctype_
  427.     xdef    _sys_nerr
  428.     xdef    _SysBase
  429.     xdef    _DOSBase
  430.     xdef    ___sF
  431.     xdef    ___SaveSP
  432.     xdef    _h_errno
  433.     xdef    __res
  434.  
  435.     end
  436.